package com.rui.study.test.hystrix.service;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

import static com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy.THREAD;

public class UserHystrixCommand extends HystrixCommand<String> {

    private final String name;

    public UserHystrixCommand(String name) {
//        super(HystrixCommandGroupKey.Factory.asKey("testGroup"));
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserHystrixCommand"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerEnabled(true)    // 默认true
                        .withExecutionIsolationStrategy(THREAD)
                        .withExecutionTimeoutInMilliseconds(1200)
                        .withFallbackIsolationSemaphoreMaxConcurrentRequests(10)));
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
        Thread.sleep(1000);
        return "Hello " + name;
    }

    @Override
    protected String getFallback() {
        return "Hello Failure " + name + "!";
    }
}
